package com.itheima.reggie.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.override.MybatisMapperMethod;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        //执行添加时自动填充
        strictInsertFill(metaObject,"createTime",LocalDateTime.class,LocalDateTime.now());
        strictInsertFill(metaObject,"updateTime",LocalDateTime.class,LocalDateTime.now());
        strictInsertFill(metaObject,"createUser",Long.class,BaseContext.getCurrentId());
        strictInsertFill(metaObject,"updateUser",Long.class,BaseContext.getCurrentId());
//        metaObject.setValue("createUser",BaseContext.getCurrentId());
//        metaObject.setValue("createTime", LocalDateTime.now());
//        metaObject.setValue("updateUser",BaseContext.getCurrentId());
//        metaObject.setValue("updateTime",LocalDateTime.now());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        //执行修改时自动填充
        metaObject.setValue("updateUser",BaseContext.getCurrentId());
        metaObject.setValue("updateTime",LocalDateTime.now());
    }
}
